﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;
using BKH.MyUserControl;

namespace BKH.MyUserControl.UCHienThi
{
    public partial class UCDanhSachCho : UserControl
    {
        String ID_Co;
        public UCDanhSachCho()
        {
            InitializeComponent();
            showDsCho();
        }
        void showDsCho()
        {
            HospitalDBDataContext context = new HospitalDBDataContext();
            var list = context.tbl_BenhNhanNoiTrus.Join(context.tbl_BenhNhan_LanKhams,
                c => c.LanKhamID,
                o => o.LanKhamID,
                (c, o) => new
                {
                    BenhNhanID = o.BenhNhanID,
                    BedID = c.BedID,
                    Date_Start = c.Date_Start,
                    Date_Finish = c.Date_Finish,
                    Date_Wait = c.Date_Wait,
                    isWait = c.IsWait

                }).Join(context.tbl_BenhNhans,
                a => a.BenhNhanID,
                p => p.BenhNhanID,
                (a, p) => new
                {
                    Id = a.BenhNhanID,
                    Ho = p.FirstName,
                    Ten = p.LastName,
                    BedID = a.BedID,
                    Date_Start = a.Date_Start,
                    Date_Finish = a.Date_Finish,
                    Date_Wait = a.Date_Wait,
                    isWait = a.isWait
                }
                ).Where(a => a.isWait == (int)Wait.Wait).Join(context.tbl_Giuongs,
                e => e.BedID,
                f => f.BedID,
                (e, f) => new
                {
                    Id=e.Id,
                    Ho=e.Ho,
                    Ten=e.Ten,
                    BedName=f.TenGiuong,
                    NgayBatDauCho=e.Date_Wait,
                    NgayRaVien=e.Date_Finish,
                    NgayVaoVien=e.Date_Start
                }
                );
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = list;
        }

        private void chuyểnVàoViệnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HospitalDBDataContext context=new HospitalDBDataContext();
            //var list = context.tbl_BenhNhanNoiTrus.Join(
            //    context.tbl_BenhNhan_LanKhams,
            //    a => a.LanKhamID,
            //    b => b.LanKhamID,
            //    (a, b) => new
            //    {
            //        BenhhNhanID=b.BenhNhanID,
            //        Date_Wait=a.Date_Wait

            //    }
            //    ).Where(n=>n.BenhhNhanID==int.Parse(ID_Co)).FirstOrDefault();

            //DateTime dt = (DateTime)list.Date_Wait;
            DateTime now = DateTime.Parse(DateTime.Now.ToShortDateString());
            //if (DateTime.Compare(dt,now)>0)
            //{
            //    MessageBox.Show("Chưa tới thời gian nhập viện cho bệnh nhân này");
            //}

            var list2 = context.tbl_BenhNhanNoiTrus.Where(n=>n.IsWait==(int)Wait.Wait && DateTime.Compare((DateTime)n.Date_Start,now)==0);
            if (list2.ToList().Count==0)
            {
                MessageBox.Show("Không có bệnh nhân nào chờ nhập viện trong ngày này");
            }
            else
            {
                ucDanhSachChoNhapVienTrongNgay ds = new ucDanhSachChoNhapVienTrongNgay();
                ControlAcess.ShowConTrol(this.ParentForm, ds, "gbHienThi");
            }

            //ucNhapVienBNTrongDSCho uc = new ucNhapVienBNTrongDSCho(int.Parse(ID_Co));
            //ControlAcess.ShowConTrol(this.ParentForm, uc, "gbHienThi");

        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void lịchSửKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucLichSuKham ls = new ucLichSuKham(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, ls, "gbHienThi");
        }

        private void ngườiNhàToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucThongTinNguoiNha info = new ucThongTinNguoiNha(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, info, "gbHienThi");
        }

        //void insertBNNoiTru()
        //{
        //    HospitalDBDataContext context = new HospitalDBDataContext();
        //    tbl_BenhNhanNoiTru tblBNNoiTru = new tbl_BenhNhanNoiTru();
        //    BenhNhanNoiTruBUO bnNoiTruBUO = new BenhNhanNoiTruBUO();
        //    tblBNNoiTru.LanKhamID = lanKhamId;
        //    tblBNNoiTru.BedID = int.Parse(cbbGiuong.SelectedValue.ToString());
        //    tblBNNoiTru.Date_Start = DateTime.Now.ToShortDateString();
        //    tblBNNoiTru.Date_Finish = dateTimePicker2.Text;
        //    tblBNNoiTru.Date_Wait = "";
        //    tblBNNoiTru.IsWait = (int)Wait.notWait;
        //    if (bnNoiTruBUO.Insert(tblBNNoiTru))
        //    {
        //        MessageBox.Show("Nhập viện thành công");
        //    }
        //    else
        //    {
        //        MessageBox.Show("Nhập viện ko thành công");
        //    }
        //}

    }
}
